﻿using System;
using Microsoft.Xna.Framework;

namespace FITClub.Util
{
#if WINDOWS

    [Serializable]
#endif
    public class MatrixTransforms
    {
        /// <summary>
        /// Position.
        /// </summary>
        public Vector3 Translation { get; set; }

        /// <summary>
        /// Rotation in angles.
        /// </summary>
        public Vector3 Rotation { get; set; }

        /// <summary>
        /// Scale.
        /// </summary>
        public Vector3 Scale { get; set; }

        /// <summary>
        /// Computes transform matrix using valid translation, rotation and scale.
        /// </summary>
        /// <param name="m">Default matrix</param>
        /// <returns></returns>
        public Matrix CreateTransform(Matrix m)
        {
            if (this.Scale.X > 0 && this.Scale.Y > 0 && this.Scale.Z > 0)
            {
                m *= Matrix.CreateScale(this.Scale);
            }
            if (this.Rotation.X != 0)
            {
                m *= Matrix.CreateRotationX(MathHelper.ToRadians(this.Rotation.X));
            }
            if (this.Rotation.Y != 0)
            {
                m *= Matrix.CreateRotationY(MathHelper.ToRadians(this.Rotation.Y));
            }
            if (this.Rotation.Z != 0)
            {
                m *= Matrix.CreateRotationZ(MathHelper.ToRadians(this.Rotation.Z));
            }
            if (this.Translation != Vector3.Zero)
            {
                m *= Matrix.CreateTranslation(this.Translation);
            }
            return m;
        }

        /// <summary>
        /// Computes transform matrix using valid translation, rotation and scale.
        /// Default is identity matrix.
        /// </summary>
        /// <returns></returns>
        public Matrix CreateTransform()
        {
            return CreateTransform(Matrix.Identity);
        }

        public MatrixTransforms()
        {
            this.Scale = Vector3.One;
        }
    }
}